package game;

import java.awt.Color;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JWindow;

public class Console extends JPanel implements ActionListener{

	private static final long serialVersionUID = 1L;
	protected JTextField input;
	protected JTextArea output;
	protected ArrayList<String> lines;
	protected Level lvl;
	protected String newLine = "\n";
	protected JFrame console;
	protected JScrollPane scroll;
	private JTextArea input1;

	public Console(Level g){
		super(new GridBagLayout());
		console = new JFrame();
		lvl = g;
		input = new JTextField(20);
		input.addActionListener(this);
		input.setCaretColor(Color.green);
		input.setForeground(Color.green);
		input.setBackground(Color.black);
		output = new JTextArea(20,40);
		input1 = new JTextArea(1,5);
		input1.setEditable(false);
		input1.setBackground(Color.black);
		input1.setForeground(Color.green);
		input1.append(">");
		input.add(input1);
		output.setEditable(false);
		output.setForeground(Color.green);
		output.setBackground(Color.black);
		scroll = new JScrollPane(output);
		scroll.setHorizontalScrollBar(null);
		GridBagConstraints c = new GridBagConstraints();
		c.gridwidth = GridBagConstraints.REMAINDER;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1.0;
		c.weighty = 1.0;
		add(scroll, c);
		c.fill = GridBagConstraints.HORIZONTAL;
		add(input, c);
		//create();
		output.append("    COMMAND PROMPT  \n");
		output.append("  __________________\n");
		print("");
		print("type in help for a list of commands");
		print("");
	}
	
	public void actionPerformed(ActionEvent e){
		output.setCaretColor(Color.GREEN);
		readCommand(input.getText());
		input.setText("");
	}

	public void print(String output){
		if(output.equals(""))
			this.output.append(newLine);
		else{
			this.output.append(" -" + output + newLine);
		}
	}

	public void create(){
		console.add(new Console(lvl));
		console.pack();
		console.setResizable(false);
		console.setIconImage(null);
		console.setTitle("COMMAND PROMPT");
		console.setVisible(true);
	}

	public void readCommand(String command){
		command.toLowerCase();
		if(command.equals("help")){
			print("load [level name] - loads level");
			print("save - saves game");
			print("restart - restarts level");
			print("exit - exits game");
			print("remenm - removes all enemies");
			print("remplat - removes all platforms");
			print("noclip - toggles noclip");
			print("help - displays this message");
			print("saveas [level name] - save game as lvl file");
			print("listlvl - lists avalable levels");
			print("disprop - displays properties");
			print("");
		}else if(command.equals("save")){
			print("level saved");
			print("");
			lvl.save();
		}else if(command.length() >= 6 && command.substring(0,4).equals("load")){
			String file = command.substring(5, command.length());
			Level newlvl = new Level(lvl.canvas, new File("levels/" + file + ".lvl"));
			print("loading " + file + "...");
			lvl.canvas.level = newlvl;
			boolean succ = lvl.canvas.level.read();
			if(succ){
				print("level loaded");
			}
			else
				print("level not found");
			print("");
		}else if(command.equals("restart")){
			print("restarting....");
			print("");
			lvl.read();
			lvl.canvas.restartGame();
		}else if(command.equals("exit")){
			print("good bye");
			print("");
			lvl.canvas.quitGame();
		}else if(command.equals("remenm")){
			lvl.enemies.removeAll(lvl.enemies);
			print("enemies removed");
			lvl.canvas.restartGame();
			print("");
		}else if(command.equals("remplat")){
			lvl.platforms.removeAll(lvl.platforms);
			print("platforms removed");
			lvl.canvas.restartGame();
			print("");
		}else if(command.equals("noclip")){
			if(lvl.canvas.noClip){
				lvl.canvas.noClip = false;
				print("no-clip disabled");
				print("");
			}else if(!lvl.canvas.noClip){
				lvl.canvas.noClip = true;
				print("no-clip enabled");
				print("");
			}
			lvl.canvas.resumeGame();
			console.remove(console);
		}else if(command.equals("die")){
			print("I'm affraid I can't do that >:]");
			print("Installing virus....");
			int temp = 0;
				JOptionPane.showMessageDialog(this, "(*@&(*&$(*@&(*$&@(*&$(*&@#*(&#@(*VIRUS DETECTED", "ERROR", JOptionPane.ERROR_MESSAGE);
			wait(5.0);
			while(temp <= 100){
			lvl.canvas.requestDefaultFocus();
			lvl.canvas.bufGraphics.setColor(Color.black);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			lvl.canvas.bufGraphics.setColor(Color.white);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			lvl.canvas.bufGraphics.setColor(Color.green);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			lvl.canvas.bufGraphics.setColor(Color.magenta);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			lvl.canvas.bufGraphics.setColor(Color.blue);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			lvl.canvas.bufGraphics.setColor(Color.orange);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			lvl.canvas.bufGraphics.setColor(Color.yellow);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			lvl.canvas.bufGraphics.setColor(Color.red);
			lvl.canvas.bufGraphics.fillRect(0, 0, lvl.canvas.screen.width, lvl.canvas.screen.height);
			wait(.025);
			temp++;
			}
			lvl.canvas.fps = 10;
		}else if(command.length() >= 6 && command.substring(0,6).equals("saveas")){
			String file = command.substring(7, command.length());
			File newLvl = new File("levels/" + file + ".lvl");
			print("saving " + file + ".lvl...");
			boolean succ = lvl.canvas.level.saveAs("levels/"+ file + ".lvl");
				if(succ){
					print("level Created");
					print("");
				}else{
					print("level overwritten");
					print("");
				}
		}else if(command.equals("listlvl")){
			File levels = new File("levels/");
			File[] files = levels.listFiles();
			for(int i= 1; i <= files.length-1; i++){
				String filename = files[i].toString().substring(7, (int)files[i].toString().length()-4);
				String ext = files[i].toString().substring((int)files[i].toString().length()-4,(int)files[i].toString().length());
				if(!filename.equals("") && ext.equals(".lvl"))
					print(filename);
			}
			print("");
		}else if(command.equals("disprop")){
			lvl.toggleProp();
			print("properties displayed");
			print("");
		}else{
			print("command not found");
			print("");
		}
	}	
	public void comeInView(){
		if(console.getState() == Frame.ICONIFIED){
			console.setState(Frame.NORMAL);
		}else{
			console.toFront();
		}
	}
	
	public static void wait (double k){
		long time0, time1;
		time0 = System.currentTimeMillis();
		do{
		time1 = System.currentTimeMillis();
		}
		while ((time1 - time0) < k * 1000);
		}
}
